package com.websuites.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.websuites.utils.Hex;

public class ChunkInput 
{
	private static void enforceEOL(InputStream paramInputStream) throws IOException
	{
		if (paramInputStream.read() != 13)
		{
			throw new IOException("chunk data not ended with CR");
		}
		if (paramInputStream.read() != 10)
		{
			throw new IOException("chunk data not ended with LF");
		}
	}

	public static int readCTE(byte[] paramArrayOfByte, InputStream paramInputStream) throws IOException
	{
		int chunkSize = 0;
		int pos = 0;
		int k = 0;
		
		while ((chunkSize = readChunkSize(paramInputStream)) > 0) 
		{ 
			int read;
			do 
			{ 
				read = paramInputStream.read(paramArrayOfByte, pos, chunkSize);
				pos += read;
				if (pos > paramArrayOfByte.length) 
				{
					throw new IOException("Max buffer exceeded");
				}
				chunkSize -= read;
			}while (chunkSize > 0);
			k += read;
			enforceEOL(paramInputStream);
		}
		return k;
	}
	
	public static int readCTE(OutputStream paramOutputStream, InputStream paramInputStream) throws IOException
	{
		int j = 0;
		boolean bool = true;
		int i;
		while ((i = readChunkSize(paramInputStream)) > 0) 
		{
			if (readAndWriteChunk(i, paramOutputStream, paramInputStream, bool) == -1)
			{
				bool = false;
			}
			j += i + 2;
		}
		return j;
	}
	
	public static int readChunkSize(InputStream paramInputStream) throws IOException
	{
		StringBuilder localStringBuilder = new StringBuilder();
		int i = 0;
		int j;
		while (((j = paramInputStream.read()) != -1) && ((j != 13) || ((j = paramInputStream.read()) != 10))) 
		{
			char c = (char)j;
			if (Hex.isHexChar(c))
			{
				localStringBuilder.append(c);
			}
		}
		i = Integer.parseInt(localStringBuilder.toString(), 16);
		
		if (i == 0)
		{
			enforceEOL(paramInputStream);
		}
		return i;
	}
	
	/** @deprecated */
	public static int readAndWriteChunk(int paramInt, OutputStream paramOutputStream, InputStream paramInputStream) throws IOException
	{
		return readAndWriteChunk(paramInt, paramOutputStream, paramInputStream, true);
	}
	
	public static int readAndWriteChunk(int paramInt, OutputStream paramOutputStream, InputStream paramInputStream, boolean paramBoolean) throws IOException
	{
		int i = paramInt;
		int j = 0;
		byte[] arrayOfByte = new byte[Math.min(paramInt, 8192)];
		while (i > 0) 
		{
			j = paramInputStream.read(arrayOfByte, 0, Math.min(arrayOfByte.length, i));
			if (j == -1)
			{
				throw new IOException("unexpected EOF, expected to read: " + paramInt + " actually read: " + j);
			}
			if (paramBoolean) 
			{
				try 
				{
					paramOutputStream.write(arrayOfByte, 0, j);
				}
				catch (IOException localIOException)
				{
					paramBoolean = false;
					throw new WriteClientIOException("Error in writing to client");
				}
			}
			i -= j;
		}
		enforceEOL(paramInputStream);
		
		if (!paramBoolean) 
		{
			return -1;
		}
		return paramInt;
	}
}
